package com.carlinfo.hospital.dao.impl;

import com.carlinfo.hospital.dao.DoctorDao;
import com.carlinfo.hospital.pojo.Doctor;
import com.carlinfo.hospital.pojo.Doctor_person;
import com.carlinfo.hospital.util.DataSourceUtil;
import com.carlinfo.hospital.util.PageInfoUtil;
import com.carlinfo.hospital.util.ReflectUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class DoctorDaoImpl implements DoctorDao
{
    QueryRunner queryRunner = new QueryRunner(DataSourceUtil.getInstance());
    private String sql = "";
    @Override
    public int saveOneDao(Doctor doctor)
    {
        sql = "insert into doctor values(null,?,?,?,?,?,?,?,?,?,?,?,?)";
        try
        {
            Object[] objects = ReflectUtil.zh(doctor);
            int i = queryRunner.execute(sql, objects);
            return i;
        } catch (SQLException e)
        {
            e.printStackTrace();
        }
        return 0;


    }

    @Override
    public List<Doctor> selectListDao(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
    {
        sql="select * from doctor where 1=1 ";
        List<Object> list = new ArrayList<Object>();
        if(condMap.get("keyword")!=null && !"".equalsIgnoreCase(condMap.get("keyword")+""))
        {
            sql+=" and name like ? ";
            list.add("%"+condMap.get("keyword")+"%");
        }
        if(condMap.get("typeId")!=null && !"".equalsIgnoreCase(condMap.get("typeId")+""))
        {
            sql += " and typeId like ? ";
            list.add("%"+condMap.get("typeId")+"%");
        }
        if(condMap.get("stDate")!=null && !"".equalsIgnoreCase(condMap.get("edDate")+""))
        {
            sql+=" and (publicTime>? and publicTime<?)";
            list.add(condMap.get("stDate"));
            list.add(condMap);
        }
        System.out.println("sql:"+sql);
        String[] s = new String[list.size()];
        String[] strings = list.toArray(s);
        try
        {
            List<Doctor> res = queryRunner.query(sql, new BeanListHandler<Doctor>(Doctor.class), strings);
            return res;
        } catch (SQLException e)
        {
            e.printStackTrace();
        }
        return null;

    }

    @Override
    public Doctor selectOneDao(Map<String, Object> condMap)
    {
        List<Object> list = new ArrayList<Object>();
        if(!"".equalsIgnoreCase(condMap.get("name")+"")&&condMap.get("name")!=null)
        {
            sql="select * from doctor where name= ? ";
            list.add(condMap.get("name"));
        }
        if(!"".equalsIgnoreCase(condMap.get("id")+"")&&condMap.get("id")!=null)
        {
            sql="select * from doctor where id= ? ";
            list.add(condMap.get("id"));
        }
        if(!"".equalsIgnoreCase(condMap.get("username")+"")&&condMap.get("username")!=null&&!"".equalsIgnoreCase(condMap.get("password")+"")&&condMap.get("password")!=null)
        {
            sql="select * from doctor where username=? and password =?";
            list.add(condMap.get("username"));
            list.add(condMap.get("password"));
        }
        String[] strings  =new String[list.size()];
        list.toArray(strings);
        System.out.println(strings[0]);
        try
        {
            Doctor res = queryRunner.query(sql, new BeanHandler<Doctor>(Doctor.class),strings);
            return res;
        } catch (SQLException e)
        {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public int updateOneDao(Doctor doctor)
    {
        sql = "update doctor set name=?,sex=?,age=?,username=?,password=?,phone=?,status=?,typeId=?,keyword=?,publicTime=?,createTime=?,updateTime=? where id =?";
        try
        {
            int execute = queryRunner.execute(sql, ReflectUtil.zh(doctor));
            return execute;
        } catch (SQLException e)
        {
            e.printStackTrace();
        }
        return 0;

    }

    @Override
    public int deleteOneDao(Map<String, Object> condMap)
    {

        List<Object> list = new ArrayList<Object>();
        if(!"".equalsIgnoreCase(condMap.get("name")+"")&&condMap.get("name")!=null)
        {
            sql="delete * from doctor where name= ? ";
            list.add(condMap.get("name"));
        }
        if(!"".equalsIgnoreCase(condMap.get("id")+"")&&condMap.get("id")!=null)
        {
            sql="delete * from doctor where id= ? ";
            list.add(condMap.get("id"));
        }
        String[] strings = new String[list.size()];
        try
        {
            int execute = queryRunner.execute(sql, list.toArray(strings));
            return execute;
        } catch (SQLException e)
        {
            e.printStackTrace();
        }
        return 0;

    }
}
